package org.onvivals.admin.module.oss.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.onvivals.common.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Mr.ZS
 * @Date: 2024/10/28 下午6:24
 **/
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "文件上传")
@RequestMapping("/oss")
public class UploadController {

	private final FileStorageService fileStorageService;

	@Operation( summary = "上传文件")
	@PostMapping("/upload")
	public Result<Object> create(@RequestParam("file") MultipartFile file) {
		String[] originalFilename = file.getName().split("\\.");
		String dateStr = DateUtil.today();
		String path = dateStr + "/";
		String fileName = originalFilename[0] + "-" + IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
		String objectName = path + fileName;
		FileInfo upload = fileStorageService.of(file, objectName).upload();
		return Result.OK(upload.getUrl());
	}

}
